<?php
namespace App\Service;

class ShelfService extends BaseService
{
    //货架

    // type=1货架，2过道，3牌子
    protected static $types = [
        1 => '货架', '过道', '牌子',
    ];

    //列表
    public static function index()
    {
        $res = self::dbConn(self::$dbShelf)
            ->where('is_able', 0)
            ->groupBy('xuhao')
            ->orderBy('id', 'asc')
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有货架');
        }
        $types = self::$types;
        $arys = [];
        foreach ($res as $v) {
            $rows = self::dbConn(self::$dbShelf)
                ->where('is_able', 0)
                ->where('xuhao', $v->xuhao)
                ->orderBy('id', 'asc')
                ->get();
            $rowList = [];
            if (count($rows)) {
                foreach ($rows as $vv) {
                    $type_name = array_key_exists($vv->type, $types) ? $types[$vv->type] : '';
                    $cengOnes = self::dbConn(self::$dbCangwei)
                        ->where('del', 0)
                        ->where('pid', 0)
                        ->where('shelf_id', $vv->id)
                        ->get();
                    $ceng_str = isset($cengOnes[0]) ? $cengOnes[0]->title : '';
                    $ceng_end = count($cengOnes)>0 ? $cengOnes[count($cengOnes)-1]->title : '';
                    $ceng_str = $ceng_end==$ceng_str ? $ceng_str : $ceng_str.'~'.$ceng_end;
                    $rowList[] = [
                        'id' => $vv->id, 'title' => $vv->title,
                        'type' => $vv->type, 'type_name' => $type_name,
                        'ceng_str' => $ceng_str,
                    ];
                }
            }
            $arys[] = [
                'xuhao' => $v->xuhao, 'rowList' => $rowList,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //新增货架
    public static function add($type)
    {
        if (!in_array($type, [1,2])) {
            return self::toback(-2, '类型错误');
        }
        $res = self::dbConn(self::$dbShelf)
            ->where('is_able', 0)
            ->groupBy('xuhao')
            ->orderBy('xuhao', 'desc')
            ->first();
        $xuhao = $res->xuhao + 1;
        $pas = [];
        if ($type == 1) {
            $pas[] = ['type' => 1, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 1, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 1, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 1, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 3, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 2, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 2, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 3, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 1, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 1, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 1, 'xuhao' => $xuhao, 'addtime' => toDate()];
            $pas[] = ['type' => 1, 'xuhao' => $xuhao, 'addtime' => toDate()];
        } else {
            for ($i = 0; $i < 12; $i ++) {
                $pas[] = ['type' => 2, 'xuhao' => $xuhao, 'addtime' => toDate()];
            }
        }
        self::dbConn(self::$dbShelf)->insert($pas);
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbShelf)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '没有货架');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbShelf)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}